package com.demo.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 单号生成
 * 
 * @author admin
 *
 */
public class OrderCodeFactory {
	/**
	 * 生成时间�?
	 * @return
	 */
	private static String getDateTime(){
		DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sdf.format(new Date());	
	}
	/**
	 * 生成单号（格式：年月日时分秒毫秒+2位随机数�?
	 * @param qz 前缀
	 * @return
	 */
	public static String createCode(String qz){
		StringBuilder idsbs = new StringBuilder();
		idsbs.append(qz);
		idsbs.append(getDateTime());
		idsbs.append(getRandom(2));
		return idsbs.toString();
	}
	
	/**     
	 * 生成固定长度随机码�?    
	 * @param n    长度     
	 * */
	 private static long getRandom(long n) {
		 long min = 1,max = 9;
		 for (int i = 1; i < n; i++) {
			 min *= 10;
			 max *= 10;
		 }
		 long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min ;
		 return rangeLong;
	 }

}
